<?php
class ModelLocalisationBank extends Model {
	public function addBank($data) {
		$sql = "INSERT INTO " . DB_PREFIX . "bank SET bank_name = '" . $this->db->escape($data['bank_name']) . "', bank_account = '" . $this->db->escape($data['bank_account']) . "', bank_account_name = '" . $this->db->escape($data['bank_account_name']) . "', currency_id = '" . (int)$data['currency_id'] . "', fee = '" . (int)$data['fee'] . "', status = '" . (int)$data['status'] . "', date_added = now()";
		
      	$this->db->query($sql);
      			
		$exchange_bank_id = $this->db->getLastId();
		
		$this->cache->delete('bank');
	}
	
	public function editBank($bank_id, $data) {
		
		$sql = "UPDATE " . DB_PREFIX . "bank SET bank_name = '" . $this->db->escape($data['bank_name']) . "',  bank_account = '" . $this->db->escape($data['bank_account']) . "', bank_account_name = '" . $this->db->escape($data['bank_account_name']) . "', currency_id = '" . (int)$data['currency_id'] . "', fee = '" . (int)$data['fee'] . "', status = '" . (int)$data['status'] . "', date_added = now() WHERE bank_id = '" . (int)$bank_id . "'";
      	$this->db->query($sql);
		
		$this->cache->delete('bank');
	}
	
	public function deleteBank($exchange_bank_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "bank WHERE bank_id = '" . (int)$exchange_bank_id . "'");
			
		$this->cache->delete('bank');
	}	
		
	public function getBanks($data = array()) {
		if ($data) {
			$sql = "SELECT b.*, c.code AS currency FROM " . DB_PREFIX . "bank b LEFT OUTER JOIN " . DB_PREFIX . "currency c ON (b.currency_id = c.currency_id)";

			
			$sort_data = array(
				'sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY sort_order";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
			
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}					

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}				
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$exchange_bank_data = $this->cache->get('bank');
		
			if (!$exchange_bank_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "bank ORDER BY sort_order");
	
				$exchange_bank_data = $query->rows;
			
				$this->cache->set('bank', $exchange_bank_data);
			}
		 
			return $exchange_bank_data;
		}
	}

	public function getBank($bank_id) {
				
		$sql = "SELECT * FROM " . DB_PREFIX . "bank WHERE bank_id = '" . (int)$bank_id . "'";

		$query = $this->db->query($sql);

		return $query->rows[0];
	}
	
	public function getTotalBanksByBankId($bank_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "bank WHERE bank_id = '" . (int)$bank_id . "'");

		return $query->row['total'];
	}

	public function getTotalBanks() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "bank");
		
		return $query->row['total'];
	}	
}
?>